<template>
  <div v-if="type === 'button'" class="button-primary flex-inline flex-center" :class="[hasHoverBg && 'button-hover-bg']"  @click="handleButtonClick">
    <span class="button-text">{{ data.text }}</span>
    <span class="button-icon"></span>
  </div>
  <div v-else class="link-button flex-inline flex-center" @click="handleButtonClick">
    <span class="link-text">{{ data.text }}</span>
    <span class="button-icon link-icon"></span>
  </div>
</template>

<script setup lang="ts">
const props = defineProps({
  type: {
    type: String,
    default: 'button', // button | link
  },

  data: {
    type: Object,
    default: () => {},
  },

  hasHoverBg: {
    type: Boolean,
    default: true
  },
})

const emit = defineEmits(['handleClick'])

// 内部跳转
const handleButtonClick = () => {
  if (!props.data.url) return
  if (props.data.url.startsWith('http')) {
    if (typeof window !== 'undefined') {
      window.open(props.data.url, '_blank')
    }
  } else {
    emit('handleClick', props.data.url)
  }
  
}
</script>

<style scoped lang="scss">
.button-icon {
  width: 18px;
  height: 18px;
  border-radius: $radius-full;
  overflow: hidden;
  background-color: $bg-primary;
  position: relative;
  margin-left: $spacing-xs;
  transition: background-color $transition-normal;
  flex-shrink: 0;

  &::before,
  &::after {
    content: "";
    width: 6px;
    height: 9px;
    position: absolute;
    top: 50%;
    left: 57%;
    background-image: url();
    background-position: 50%;
    background-size: cover;
    background-repeat: no-repeat;
    background-color: hsla(0,0%,100%,0);
    transition: transform .3s ease-in-out;
  }

  &::before {
    transform: translate(-260%,-50%);
  }

  &::after {
    transform: translate(-50%,-50%);
  }
}

.link-icon {
  background-color: $primary-color;

  &::before,
  &::after {
    background-image: url();
  }
}

.link-button {
  overflow: hidden;
  text-decoration: none;
  position: relative;
  z-index: 1;
  white-space: nowrap;
  cursor: pointer;
  transition: all $transition-normal;

  .link-text {
    font-weight: $font-weight-medium;
    letter-spacing: .04em;
    color: $text-important;
    transition: color $transition-normal;
  }

  &::before {
    content: "";
    display: block;
    position: absolute;
    width: 200%;
    height: 500%;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    transition: all $transition-normal;
    z-index: -1;
    -webkit-clip-path: circle(100% at -80% 90%);
    clip-path: circle(100% at -80% 90%);
  }

  &:hover::before {
    -webkit-clip-path: circle(100% at 50% 50%);
    clip-path: circle(100% at 50% 50%);
  }

  &:hover {
    .button-icon:before {
      transform: translate(-50%,-50%);
    }

    .button-icon:after {
      transform: translate(150%,-50%);
    }

    .button-icon:after,
    .button-icon:before {
      background-image: url();
      background-position: 50%;
      background-size: cover;
      background-repeat: no-repeat;
      background-color: hsla(0,0%,100%,0);
    }
  }
}

.button-primary {
  @extend .link-button;
  background-color: $button-bg;
  padding: 11px 15px;
  min-width: 160px;
  border-radius: $radius-sm;
  box-shadow: $shadow-sm;

  .button-text {
    font-weight: $font-weight-medium;
    letter-spacing: .04em;
    color: $text-white;
    transition: color $transition-normal;
  }

  .button-icon:after,
  .button-icon:before {
    background-image: url() !important;
  }
}

.button-hover-bg {
  &::before {
    background: $button-hover-bg;
  }
}

@media (max-width: 1023.98px) {
  .button-primary {
    padding: 11px 15px;
    min-width: 100px;
  }
}

@media (max-width: 767.98px) {
  .button-primary {
    padding: 8px 12px;
    min-width: 80px;
  }
}

@media (max-width: 1023.98px) {
  .button-primary {
    padding: 11px 15px;
    min-width: 100px;
  }
}

@media (max-width: 767.98px) {
  .button-primary {
    padding: 8px 12px;
    min-width: 80px;
  }
}

@media (max-width: 767.98px) {
  .button-icon {
    width: 12px;
    height: 12px;
    margin-left: 4px;
  }

  .button-icon:after,
  .button-icon:before {
    width: 4.7px;
    height: 6px;
    background-image: url();
    background-position: 50%;
    background-size: cover;
    background-repeat: no-repeat;
    background-color: hsla(0,0%,100%,0);
  }
}
</style>